package com.xjj.consumer;

import com.rabbitmq.client.Channel;
import com.xjj.service.VoteService;
import com.xjj.vo.VoteVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @ClassName VoteConsumer
 * @Description TODO
 * @Author GH
 * @Date 2022/10/26 17:17
 * @Version 1.0
 **/
@Component
@Slf4j
public class VoteConsumer {
    @Resource
    private VoteService voteService;

    @RabbitListener(queues = "voteTimeOut")
    public void voteTimeOutConsumer(VoteVo voteVo , Message message, Channel channel) throws IOException {
        System.out.println("voteVo:"+voteVo);
        System.out.println("message:"+message);
        System.out.println("channel:"+channel);
        //调用修改状态的service处理过期投票
        log.debug("消息状态:{}","消费者开始处理");
        voteService.voteTimeOut(voteVo.getVote().getId());
        //nack:true表示消费失败重回队列,flase表示消费失败不重回队列
        //ack:true表示消费成功重回队列,flase表示消费成功不重回队列
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//        //nack:true表示消费失败重回队列,flase表示消费失败不重回队列
//        //ack:true表示消费成功重回队列,flase表示消费成功不重回队列
//        channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
    }

    @RabbitListener(queues = "queuevotedlx")
    public void queueVoteDlxConsumer(VoteVo voteVo , Message message, Channel channel) throws IOException {
        System.out.println("voteVo:"+voteVo);
        System.out.println("message:"+message);
        System.out.println("channel:"+channel);
        //调用修改状态的service处理过期投票
        log.debug("消息状态:{}","消费者开始处理");
        voteService.voteTimeOut(voteVo.getVote().getId());
        //nack:true表示消费失败重回队列,flase表示消费失败不重回队列
        //ack:true表示消费成功重回队列,flase表示消费成功不重回队列
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
